package com.athena.controller 
{
	import com.adobe.serialization.json.JSON;
	import com.athena.external.bank.getB;
	import com.athena.external.bank.ItemBank;
	import com.athena.external.bank.MainMovie;
	import com.athena.utils.Delegate;
	import com.athena.utils.Util;
	import com.junkbyte.console.Cc;
	import fl.controls.RadioButton;
	import fl.controls.RadioButtonGroup;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.text.TextFormat;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	/**
	 * ...
	 * @author ...
	 */
	public class Bank extends MainMovie
	{
		private var active : String = "";
		private var itemNhan : ItemBank;
		private var rbg : RadioButtonGroup;
		public function Bank(_act : String = "nhan") 
		{
			var glow:GlowFilter = new GlowFilter();
			glow.color = 0x000000;
			glow.alpha = 0.5;
			glow.blurX = 10;
			glow.blurY = 10;
			glow.quality = BitmapFilterQuality.MEDIUM;

			filters=[glow];
			
			var _w : Number = width; var _h : Number = height;
			var rectangle:Shape = new Shape();
			rectangle.graphics.beginFill(0x000000, 1);
			rectangle.graphics.drawRect((_w - Main.getInstance().config.demension.x) / 2, (_h - Main.getInstance().config.demension.y) / 2, Main.getInstance().config.demension.x, Main.getInstance().config.demension.x);
			rectangle.alpha = 0.2;
			rectangle.graphics.endFill();
			addChild(rectangle);
			setChildIndex(rectangle, 0);
			
			this.x = (Main.getInstance().config.demension.x - _w) / 2;
			this.y = (Main.getInstance().config.demension.y - _h) / 2;
			
			setupButton();
			btnclick(null, _act);
		}
		
		private function setupButton() : void {
			
			nhan_btn.addEventListener(MouseEvent.ROLL_OVER, Delegate.create(btnover, "nhan"));
			nhan_btn.addEventListener(MouseEvent.ROLL_OUT, Delegate.create(btnout, "nhan"));
			nhan_btn.addEventListener(MouseEvent.CLICK, Delegate.create(btnclick, "nhan"));
			
			chuyen_btn.addEventListener(MouseEvent.ROLL_OVER, Delegate.create(btnover, "chuyen"));
			chuyen_btn.addEventListener(MouseEvent.ROLL_OUT, Delegate.create(btnout, "chuyen"));
			chuyen_btn.addEventListener(MouseEvent.CLICK, Delegate.create(btnclick, "chuyen"));
			
			close_btn.addEventListener(MouseEvent.CLICK, close);
			
			chuyen_mc.accept_btn.addEventListener(MouseEvent.CLICK, sendChuyen);
		}
		
		private function removeEvent() : void {
			nhan_btn.removeEventListener(MouseEvent.ROLL_OVER, Delegate.create(btnover, "nhan"));
			nhan_btn.removeEventListener(MouseEvent.ROLL_OUT, Delegate.create(btnout, "nhan"));
			nhan_btn.removeEventListener(MouseEvent.CLICK, Delegate.create(btnclick, "nhan"));
			
			chuyen_btn.removeEventListener(MouseEvent.ROLL_OVER, Delegate.create(btnover, "chuyen"));
			chuyen_btn.removeEventListener(MouseEvent.ROLL_OUT, Delegate.create(btnout, "chuyen"));
			chuyen_btn.removeEventListener(MouseEvent.CLICK, Delegate.create(btnclick, "chuyen"));
			
			close_btn.removeEventListener(MouseEvent.CLICK, close);
			
			chuyen_mc.accept_btn.removeEventListener(MouseEvent.CLICK, sendChuyen);
		}
		
		private function close(e : MouseEvent) : void {
			removeEvent();
			this.parent.removeChild(this);
		}
		private function btnover(e : MouseEvent, str: String) : void {
			Mouse.cursor = MouseCursor.BUTTON;
			if(active != str)
				(e.currentTarget as MovieClip).gotoAndStop(2);
		}
		private function btnout(e : MouseEvent, str: String) : void {
			Mouse.cursor = MouseCursor.AUTO;
			if(active != str)
				(e.currentTarget as MovieClip).gotoAndStop(1);
		}
		
		private function btnclick(e : MouseEvent, str :String) : void {
			if (str == "chuyen" && Main.getInstance().currPlayer.vip < 4) {
				Util.simpleAlert("Lên Vip 4 để sử dụng chức năng này");
				return;
			}
			if (active == str) return;
			active = str;
			nhan_btn.gotoAndStop(1);
			chuyen_btn.gotoAndStop(1);
			
			var btn : MovieClip = (str == "nhan") ? nhan_btn : chuyen_btn;
			btn.gotoAndStop(3);
			if (str == "nhan") {
				nhan_mc.visible = true;
				chuyen_mc.visible = false;
				var obj : Object = new Object() ;
				obj.evt = "15" ;
				obj.T = 6;
				obj.P = 1 ;
				Main.getInstance().socket.sendService(JSON.encode(obj));
			}else {
				nhan_mc.visible = false;
				chuyen_mc.visible = true;
				displayChuyenB();
			}
		}
		private function displayChuyenB() : void {
			if (rbg) return;
			
			var percent : int = 10;
			
			var vip : int = Main.getInstance().currPlayer.vip;
			
			if (vip == 1) percent = 8;
			if (vip > 1) percent = 6;
			if (vip > 3) percent = 5;
			if (vip > 5) percent = 4;
			if (vip > 6) percent = 3;
			if (vip > 7) percent = 2;
			if (vip > 8) percent = 1;
			if (vip > 9) percent = 0;
			
			chuyen_mc.fee_txt.text = percent + "%";
			
			rbg = new RadioButtonGroup("groupTransferMark");
			var formatText : TextFormat = new TextFormat() ;				
			formatText.font = "Tahoma" ;
			formatText.bold = true ;
			formatText.color = 0xFFFFFF ;
			formatText.size = 12 ;
			
			var arrMark : Array = [5000, 10000, 20000, 50000];
			for(var i : int = 1; i <= arrMark.length; i++) {
				var rb : RadioButton = new RadioButton;
				if (i == 1) rb.selected = true;
				rb.x = ((i - 1) % 2 == 0) ? 122 : 260;
				rb.y = 45 + Math.floor((i - 1) / 2) * 36;
				rb.width = 120;
				
				rb.setStyle("textFormat",formatText);
				rb.group = rbg;
				rb.label = Util.numberFormat(arrMark[i - 1], 0) + Main.getInstance().gameObject.UnitMark;
				rb.value = arrMark[i - 1];
				chuyen_mc.addChild(rb);
			}
			
			
		}
		
		private function sendChuyen(e : MouseEvent) : void {
			var to : String = chuyen_mc.to_txt.text;
			if (to == "") {
				Main.getInstance().stage.focus = chuyen_mc.to_txt;
				return;
			}
			var m : int = (rbg as Object).selection.value;
			var obj : Object = new Object;
			obj.AG = m;
			obj.Name = to ;
			obj.evt = "10";
			Main.getInstance().socket.sendService(JSON.encode(obj));
			Util.showLoading();
		}
		
		public function TransferMark(data : String) : void {
			var arr : Array = JSON.decode(data);
			displayList(arr);
		}
		
		private function displayList(arr : Array) : void {
			var len : int = arr.length;
			for (var i : int = 0; i < len; i++ ) {
				var item : ItemBank = new ItemBank(arr[i].Id, arr[i].From, arr[i].Msg, arr[i].AG, Util.numberFormat(arr[i].AG, 0),arr[i].Time, arr[i].S);
				item.x = 7.55;
				item.y = 1.85 + i * 24.25;
				nhan_mc.list_mc.addChild(item);
				item.addEventListener(ItemBank.ITEMVIEW, Delegate.create(itemView, item));
			}
		}
		
		private function itemView(e : Event, item : ItemBank) : void {
			if (!item.read) {
				item.setRead();
				var obj :Object = new Object() ;
				obj.evt = "15" ;
				obj.T = 1 ;
				obj.ID = item.id;
				Main.getInstance().socket.sendService(JSON.encode(obj));
			}
			var view : getB = new getB(item.id, item.from, item.message, Util.numberFormat(item.mark, 0) + Main.getInstance().gameObject.UnitMark);
			view.addEventListener(getB.ACCEPT, Delegate.create(sendGetMark, item));
			addChild(view);
		}
		private function sendGetMark(e  : Event, item : ItemBank) : void {
			var obj :Object = new Object() ;
			obj.evt = "15" ;
			obj.T = 2 ;
			obj.Arr = [item.id];
			Main.getInstance().socket.sendService(JSON.encode(obj));
			itemNhan = item;
			Util.showLoading();
		}
		public function getAGSuccess(mak : int)  :void {
			Util.removeLoad();
			Main.getInstance().dashBoard.updateMark(mak);
			if (itemNhan) {
				nhan_mc.list_mc.removeChild(itemNhan);
				itemNhan = null;
			}
			
			var len : int = nhan_mc.list_mc.numChildren;
			
			for (var i : int = 0 ; i < len; i++ ) {
				nhan_mc.list_mc.getChildAt(i).y = 1.85 + i * 24.25;
			}
		}
	}

}